package com.xy.modular.shop.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.util.uuid.UUIDUtils;
import com.xy.modular.shop.entity.ProductProperty;
import com.xy.modular.shop.service.IProductPropertyService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 7. 商品 属性名 属性值 关联表 property,property表其实就是属性名表和属性值的合并 初看数据库认为此表与8表重复，其实不然，因为is_sale是否为销售属性，
如果不是销售属性，前端就可以直接读取此表数据，如果是销售属性就要读取sku表数据了 前端控制器
 * </p>
 *
 * @author BCK
 * @since 2019-10-12
 */
@Controller
@RequestMapping("/shop/product-property")
public class ProductPropertyController extends BaseController {
	static Logger log = LoggerFactory.getLogger(ProductPropertyController.class);

	@Autowired
	private IProductPropertyService productPropertyService;

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject listModule(ProductProperty productProperty, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		return new MReturnObject(MReturnObject.SUCCESS, productPropertyService.findColumnsPageAll(pageRequest),
				productPropertyService.getCount(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MReturnObject save(ProductProperty productProperty) {
		try {
			productPropertyService.validateEntity(productProperty);
			productProperty.setId(UUIDUtils.UUID());
			productPropertyService.save(productProperty);
		} catch (Exception e) {
			log.info("ProductProperty保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(ProductProperty productProperty) {
		try {
			productPropertyService.validateEntity(productProperty);
			productPropertyService.update(productProperty);
		} catch (Exception e) {
			log.info("ProductProperty修改失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody MReturnObject del(@PathVariable String id) {
		try {
			productPropertyService.deleteById(id);
		} catch (Exception e) {
			log.info("ProductProperty删除失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}
}
